package com.tx.admin.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import com.tx.admin.controller.po.PagePo;
import com.tx.admin.entity.SuperEntity;
import com.tx.admin.repository.SuperRepository;
import com.tx.admin.service.SuperService;

public abstract class SuperServiceImpl<E extends SuperEntity> implements SuperService<E>{

	@Autowired
	private SuperRepository<E> superRepository;
	
	
	
	@Override
	public long countById(String id) {
		// TODO Auto-generated method stub
		return this.superRepository.countById(id);
	}

	@Override
	public List<E> findByIds(Collection<String> ids) {
		// TODO Auto-generated method stub
		return this.superRepository.findAllById(ids);
	}

	@Override
	public long count(PagePo<E> params) {
		// TODO Auto-generated method stub
		return this.superRepository.count(params);
	}

	@Override
	public long count() {
		// TODO Auto-generated method stub
		return this.superRepository.count();
	}

	@Override
	public E save(E entity) {
		// TODO Auto-generated method stub
		entity.init();
		superRepository.save(entity);
		return entity;
	}

	@Override
	public List<E> save(List<E> entitys) {
		// TODO Auto-generated method stub
		entitys.parallelStream().forEach(val -> {
			this.save(val);
		});
		return entitys;
	}



	@Override
	public void delete(E entity) {
		// TODO Auto-generated method stub
		this.superRepository.delete(entity);
	}

	@Override
	public void delete(List<E> entitys) {
		// TODO Auto-generated method stub
		entitys.parallelStream().forEach(val -> {
			this.delete(val);
		});
	}

	@Override
	public void deleteAll() {
		// TODO Auto-generated method stub
		this.superRepository.deleteAll();
	}

	@Override
	public Page<E> findAll(PagePo<E> params) {
		// TODO Auto-generated method stub
		return this.superRepository.findAll(params);
	}

	@Override
	public List<E> findAll() {
		// TODO Auto-generated method stub
		return this.superRepository.findAll();
	}

	@Override
	public E findById(String id) {
		// TODO Auto-generated method stub
		Optional<E> op=this.superRepository.findById(id);
		if(!op.isPresent()) {
			throw new RuntimeException();
		}
		return op.get();
	}

	
}
